#!/usr/bin/Rscript

library(fields)
library(sfsmisc)

con = file("../adcirc/fort.22")
zz = file("../adcirc/fort.14")

dummy = scan(zz,skip=1,nlines=1,quiet=T)
nele = dummy[1]
cat('nele=',nele,'\n')
nnode = dummy[2]
cat('nnode=',nnode,'\n')
xy = matrix(scan(zz,nlines=nnode,skip=2),ncol=4,byrow=T)[,2:3]

ngrids = as.integer(system("cat ../adcirc/fort.22 | wc -l",intern=T))
ngrids = floor(ngrids/nnode)

skip = 0
for(i in 1:ngrids){
    if(i > 2) skip = skip + nnode + 1
    print(skip)
    uv = matrix(scan('../adcirc/fort.22',nlines=nnode,skip=skip),ncol=4,byrow=T)[,2:3]
    plot(xy,type='n',xlim=c(-124.5, -124),ylim=c(40.4, 41))
    arrow.plot(a1=xy,a2=uv,xpd=F,arrowfun=p.arrows,size=.1)
    Sys.sleep(.1)
}
